package edu.dali.enterprise_test.controller.menu;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import edu.dali.enterprise_test.model.Pager;
import edu.dali.enterprise_test.model.dto.DishModel;
import edu.dali.enterprise_test.model.vo.Dto;
import edu.dali.enterprise_test.service.menu.MenuService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private HttpServletRequest request;

    @Value("${image.path}")
    private String imgPath;

    @GetMapping("index")
    public String index() {

        return "menu/index";
    }

    @PostMapping("getData")
    @ResponseBody
    public Pager<Dto> getData(int page , int limit) {
        PageHelper.startPage(page,limit);

        String dish = request.getParameter("dish");
        String category = request.getParameter("category");
        Dto params = new Dto();
        if (!StringUtils.isEmpty(dish)) {
            params.put("dish" , dish);
        }
        if (!StringUtils.isEmpty(category)) {
            params.put("category" , category);
        }
        Pager p = menuService.getData(params);
        return p;
    }

    @RequestMapping(value = "addDialog")
    public String addDialog() {
        return "menu/dialog/add";
    }

    @RequestMapping(value = "updateDialog/{id}")
    public String updateDialog(@PathVariable String id , Model model) throws JsonProcessingException {
        Dto dish = menuService.getDataById(id);
        String info = objectMapper.writeValueAsString(dish);
        model.addAttribute("info" , info);
        return "menu/dialog/update";
    }

    @PostMapping("update")
    @ResponseBody
    public Map<String , Object> update() {
        String id = request.getParameter("id");
        String dish = request.getParameter("dish");
        String category = request.getParameter("category");
        String stock = request.getParameter("stock");
        String img_path = request.getParameter("img_path");
        Dto dto = new Dto();
        dto.put("id" , id);
        dto.put("dish" , dish);
        dto.put("category" , category);
        dto.put("stock" , stock);
        dto.put("img_path" , img_path);
        Map<String , Object> map = new HashMap<>();
        menuService.update(dto);
        map.put("code" , 0);
        map.put("msg" , "修改成功");
        return map;
    }

    //添加菜品
    @PostMapping("add")
    @ResponseBody
    public Map<String , Object> add() {
        Map<String ,Object> res = new HashMap<>();
        String dish = request.getParameter("dish");
        String category = request.getParameter("category");
        String img_path = request.getParameter("img_path");
        String stock = request.getParameter("stock");
        Dto dto = new Dto();
        if (!StringUtils.isEmpty(dish)) dto.put("dish" , dish);
        if (!StringUtils.isEmpty(category)) dto.put("category" , category);
        if (!StringUtils.isEmpty(img_path)) dto.put("img_path" , img_path);
        if (!StringUtils.isEmpty(dish)) dto.put("stock" , stock);
        menuService.addDish(dto);
        res.put("code" , 0);
        res.put("msg" , "添加成功");
        return res;
    }

    //删除菜品
    @PostMapping("delete")
    @ResponseBody
    public Map<String , Object> delete(String id) {
        Map<String ,Object> res = new HashMap<>();
        Dto dto = new Dto();
        dto.put("id" , id);
        menuService.delete(dto);
        res.put("code" , 0);
        res.put("msg" , "删除成功!");
        return res;
    }

    @PostMapping("saveImg")
    @ResponseBody
    public Map<String , Object> saveImg(MultipartFile file) {
        Map<String , Object> res = new HashMap<>();
        if (file != null) {
            try {
                String type= file.getContentType();
                String suffix = "";
                if (type.equals("image/jpeg")) suffix = ".jpg";
                else if(type.equals("image/png")) suffix = ".png";
                else throw new RuntimeException("类型不存在!");
                String fileName = UUID.randomUUID().toString();
                file.transferTo(new File(imgPath+fileName+suffix));
                res.put("code" , 0);
                res.put("fileName" , fileName+suffix);
            } catch (IOException e) {
                res.put("code" , 1);
                e.printStackTrace();
            }
        }

        return res;
    }
}
